package edu.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.school.entity.Comment;
import edu.school.entity.Reply;
import edu.school.entity.ReplyExample;
import edu.school.entity.User;
import edu.school.mapper.CommentMapper;
import edu.school.mapper.ReplyMapper;
import edu.school.mapper.UserMapper;
import edu.school.service.ReplyService;
import edu.school.service.ReplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    ReplyMapper mapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public boolean save(Reply user) {
        return mapper.insertSelective(user)>0?true:false;
    }

    @Override
    public boolean update(Reply user) {
        return mapper.updateByPrimaryKeySelective(user)>0?true:false;
    }

    @Override
    public PageInfo<Reply> getPageList(Integer pageNum, Integer pageSize, Reply record) {
        ReplyExample example = new ReplyExample();
        example.setOrderByClause("create_time desc");
        ReplyExample.Criteria criteria = example.createCriteria();
        if (null != record) {
            if (StringUtils.isNoneBlank(record.getContent())) {//公告标题查询
               criteria.andContentLike("%" + record.getContent() + "%");
            }
            if (null!=record.getUid()){
               criteria.andUidEqualTo(record.getUid());
            }

        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<Reply> list = mapper.selectByExample(example);//第2步
        for (Reply date : list) {
            User user = userMapper.selectByPrimaryKey(date.getUid());
            date.setUser(user);
            Comment comment = commentMapper.selectByPrimaryKey(date.getCid());
            date.setComment(comment);
        }
        PageInfo<Reply> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;

    }

    @Override
    public Reply findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteReply(Integer id) {
        return mapper.deleteByPrimaryKey(id)>0?true:false;
    }

    @Override
    public int deleteMany(String[] ids) {
        return mapper.deleteMany(ids);
    }

    @Override
    public List<Reply> findAll(Integer cid) {
        ReplyExample example = new ReplyExample();
        example.setOrderByClause("create_time desc");
        ReplyExample.Criteria criteria = example.createCriteria();
        criteria.andCidEqualTo(cid);
        List<Reply> list = mapper.selectByExample(example);
        for(Reply data:list) {
            User user=userMapper.selectByPrimaryKey(data.getUid());
            data.setUser(user);
            Comment comment= commentMapper.selectByPrimaryKey(data.getCid());
            data.setComment(comment);
        }
        return list;
    }
}
